﻿using Microsoft.SharePoint;
using VH.SharePoint.Api.Entensions;

namespace VH.SharePoint.Api
{
    public class MediaItem : BlogItem, IMediaItemInfo
    {
        public MediaItem(SPListItem item) : base(item)
        {
        }

        public static implicit operator MediaItem(SPListItem item)
        {
            return new MediaItem(item);
        }

        public string Name {
            get { return Item.File.Name; }
            set
            {
                int index = -1;
                if ((index =value.LastIndexOf(".", System.StringComparison.Ordinal))!=-1)
                {
                    value = value.Substring(0, index);
                }
                Item.SetFieldValue<string>(value, SPBuiltInFieldId.FileLeafRef);
            }
        }
        public string Description { get; set; }
        public string Url {
            get { return Item.File.ServerRelativeUrl; }
            set { }
        }
        public byte[] Content { get; set; }
        public string MimeType { get; set; }

        public void Update(byte[] mediaContent)
        {
            Item.File.SaveBinary(mediaContent);
        }
    }
}